<?php

namespace app\demo\controller;

use PhpOffice\PhpWord\IOFactory;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\SimpleType\JcTable;
use PhpOffice\PhpWord\Style\Cell;
use PhpOffice\PhpWord\Style\Table;
use PhpOffice\PhpWord\TemplateProcessor;
use think\App;
use think\facade\Db;
use think\facade\View;

/**
 * 使用当前代码，需要安装扩展
 * composer require phpoffice/phpword
 */
class Word extends Base
{

    private function getValueByKey($key_str, $data)
    {
        if (empty($key_str)) {
            return "";
        }
        $keys_arr = explode(".", $key_str);

        $result = $data;
        foreach ($keys_arr as $key) {
            if (empty($result[$key])) {
                $result = "";
            } else {
                $result = $result[$key];
            }
        }
        return $result;
    }

    public function index()
    {

        $info = [
            'name' => '西湖杰克马有限公司',
            'pic1' => 'static/image/css.jpg',
            'pic2' => 'static/image/nginx.png',
            'score' => '744分',
            'desc' => '河北省高考状元',
            'info' => [
                'name' => '侯赛因.萨达姆',
            ],
            'list' => [
                ['name' => '《三国演绎》', 'author' => '罗贯中'],
                ['name' => '《水浒传》', 'author' => '施耐庵'],
            ],
        ];

        $document = new TemplateProcessor('download/template.docx');
        $document->setValue('name', $this->getValueByKey("name", $info));
        $document->setImageValue('pic1', $this->getValueByKey("pic1", $info));
        $document->setImageValue('pic2', $this->getValueByKey("pic2", $info));
        $document->setValue('score', $this->getValueByKey("score", $info));
        $document->setValue('desc', $this->getValueByKey("desc", $info));
        $document->setValue('info.name', $this->getValueByKey("info.name", $info));
        $document->setValue('list.0.name', $this->getValueByKey("list.0.name", $info));
        $document->saveAs('target.docx');


        // 创建一个新的 PHPWord 对象
//        $phpWord = new PhpWord();
//// 添加一个段落样式（可选）
//        $phpWord->addParagraphStyle('pStyle', array('align' => 'center'));
//
//        // 添加一个表格样式
//        $tableStyle = new Table();
//        $tableStyle->setCellMargin(50); // 设置单元格边距
//        $tableStyle->setAlignment(JcTable::CENTER);
//// 添加一个单元格样式
//        $cellStyle = new Cell();
//        $cellStyle->setValign('center'); // 垂直居中
//// 添加一个部分
//        $section = $phpWord->addSection();
//// 添加标题
//        $section->addText('这是一个带表格的Word文档', 'pStyle');
//// 添加表格
//        $table = $section->addTable($tableStyle);
//        $table->addRow(500); // 行高为500
//        $table->addCell(1000, $cellStyle)->addText('列1');
//        $table->addCell(1000, $cellStyle)->addText('列2');
//        $table->addCell(1000, $cellStyle)->addText('列3');
//// 添加更多行
//        for ($i = 0; $i < 5; $i++) {
//            $table->addRow(400); // 行高为400
//            $table->addCell(1000, $cellStyle)->addText("数据 $i-1");
//            $table->addCell(1000, $cellStyle)->addText("数据 $i-2");
//            $table->addCell(1000, $cellStyle)->addText("数据 $i-3");
//        }
//// 保存文档
//        $objWriter = IOFactory::createWriter($phpWord, 'Word2007');
//        $objWriter->save('HelloWorldWithTable.docx');
        return successReturn();
//        return successReturn($_SERVER);
    }


}
